//
//  ChoiceCarTableViewController.m
//  CarCare
//
//  Created by ileo on 14-9-19.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "ChoiceCarTableViewController.h"
#import "ChoiceCarInfoViewController.h"

#define CELLHEIGHT 50

@interface ChoiceCarTableViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, copy) NSArray *indexArr;
@property (nonatomic, copy) NSDictionary *dataDic;

@property (nonatomic, copy) NSArray *dataArr;

@property (nonatomic, assign) CHOICE_CAR type;

@property (nonatomic, strong) SetCarHelper *setHelper;

@end

@implementation ChoiceCarTableViewController

- (instancetype)initWithData:(id)data type:(CHOICE_CAR)type setCarHelper:(SetCarHelper *)setCarHelper
{
    self = [super initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE];
    if (self) {
        self.type = type;
        self.setHelper = setCarHelper;

        switch (type) {
            case CHOICE_CAR_BRAND:
                self.title = @"选择品牌";
                self.indexArr = [[data allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
                    return [obj1 compare:obj2 options:NSNumericSearch];
                }];
                self.dataDic = data;
                break;
            case CHOICE_CAR_MODEL:
                self.title = @"选择车系";
                self.dataArr = data;
                break;
            case CHOICE_CAR_YEAR:
                self.title = @"选择年份";
                self.dataArr = data;
                break;
            case CHOICE_CAR_STYLE:
                self.title = @"选择车型";
                self.dataArr = data;
                break;
                
            default:
                break;
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.frame = CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor = [Theme lineColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.sectionIndexColor = [UIColor grayColor];
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    self.tableView.sectionFooterHeight = 3;
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.type == CHOICE_CAR_BRAND) {
        return self.indexArr.count;
    }
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.type == CHOICE_CAR_BRAND) {
        NSString *key = self.indexArr[section];
        NSArray *arr = self.dataDic[key];
        return arr.count;
    }
    return self.dataArr.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CELLHEIGHT;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
   
    cell.textLabel.textColor = [Theme wordColor];
    cell.textLabel.font = FONT_TABLE_TITLE;
    switch (self.type) {
        case CHOICE_CAR_BRAND:
        {
            NSString *key = self.indexArr[indexPath.section];
            NSDictionary *dic = self.dataDic[key][indexPath.row];
            cell.textLabel.text = dic[@"brandName"];
        }
            break;
        case CHOICE_CAR_MODEL:
        {
            cell.textLabel.text = self.dataArr[indexPath.row][@"modelName"];
        }
            break;
        case CHOICE_CAR_YEAR:
        {
            cell.textLabel.text = self.dataArr[indexPath.row];
        }
            break;
        case CHOICE_CAR_STYLE:
        {
            cell.textLabel.text = self.dataArr[indexPath.row][@"styleName"];
        }
            break;
        default:
            break;
    }

    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak __typeof(self) wself = self;
    switch (self.type) {
        case CHOICE_CAR_BRAND:
        {
            NSString *key = self.indexArr[indexPath.section];
            NSDictionary *dic = self.dataDic[key][indexPath.row];
            self.setHelper.carInfoModel.brandName = dic[@"brandName"];
            self.setHelper.carInfoModel.brandId = [dic[@"brandId"] integerValue];
            
            [self.setHelper getCarModelSuccess:^(NSArray *models) {
                ChoiceCarTableViewController *vc = [[[ChoiceCarTableViewController alloc] initWithData:models type:CHOICE_CAR_MODEL setCarHelper:wself.setHelper] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
                [wself.navigationController pushViewController:vc  animated:YES];
            }];
        }
            break;
        case CHOICE_CAR_MODEL:
        {
            NSDictionary *dic = self.dataArr[indexPath.row];
            self.setHelper.carInfoModel.modelName = dic[@"modelName"];
            self.setHelper.carInfoModel.modelId = [dic[@"modelId"] integerValue];
            
            [self.setHelper getCarYearSuccess:^(NSArray *years) {
                ChoiceCarTableViewController *vc = [[[ChoiceCarTableViewController alloc] initWithData:years type:CHOICE_CAR_YEAR setCarHelper:wself.setHelper] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
                [wself.navigationController pushViewController:vc  animated:YES];
            }];
        }
            break;
        case CHOICE_CAR_YEAR:
        {
            self.setHelper.carInfoModel.year = self.dataArr[indexPath.row];
            
            [self.setHelper getCarStyleSuccess:^(NSArray *styles) {
                ChoiceCarTableViewController *vc = [[[ChoiceCarTableViewController alloc] initWithData:styles type:CHOICE_CAR_STYLE setCarHelper:wself.setHelper] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
                [wself.navigationController pushViewController:vc  animated:YES];
            }];
        }
            break;
        case CHOICE_CAR_STYLE:
        {
            NSDictionary *dic = self.dataArr[indexPath.row];
            self.setHelper.carInfoModel.styleName = dic[@"styleName"];
            self.setHelper.carInfoModel.carTypeId = [dic[@"styleId"] integerValue];
            for (UIViewController *vc in self.navigationController.viewControllers) {
                if ([vc isKindOfClass:[ChoiceCarInfoViewController class]]) {
                    [wself.navigationController popToViewController:vc animated:YES];
                    break;
                }
            }
        }
            break;
        default:
            break;
    }
}

//获取分组标题并显示
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (self.type == CHOICE_CAR_BRAND) {
        NSString *key = self.indexArr[section];
        return key;
    }
    return @"";
}

//给tableviewcell添加索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (self.type == CHOICE_CAR_BRAND) {
        return self.indexArr;
    }
    return nil;
}

@end